<?php
/*************************************************************************
Class Pick_Core_ObjectInfo
----------------------------------------------------------------
PickCMS
Company: Pick.vn 
Email: admin@pick.vn
Last updated: 19/08/2011
**************************************************************************/
class Pick_Core_ObjectInfo {

	var $field 		= array();
	var $lang		= "";
	
	function Pick_Core_ObjectInfo (){
		global $load, $lang;
		$this->lang = $lang;
	}
	/*-----------------------------------------------------------------------*
	* Function: getField
	* Parameter: WHERE condition
	* Return: Data will be sub array category
	*-----------------------------------------------------------------------*/	
	function getField($key) {
		//return htmlspecialchars($this->field[$key], ENT_QUOTES);
		return stripslashes($this->field[$key]);
	}	
	/*-----------------------------------------------------------------------*
	* Function: setField
	* Parameter: WHERE condition
	* Return: Data will be sub array category
	*-----------------------------------------------------------------------*/		
	function setField($key, $nValue) {
		//$this->field[$key]=htmlspecialchars($nValue, ENT_QUOTES);
		$this->field[$key]=stripslashes($nValue);
	}
	/*-----------------------------------------------------------------------*
	* Function: getArrayField
	* Parameter: WHERE condition
	* Return: Data will be sub array category
	*-----------------------------------------------------------------------*/	
	function getArrayField() {
		return $this->field;
	}
	/*-----------------------------------------------------------------------*
	* Function: setArrayField
	* Parameter: WHERE condition
	* Return: Data will be sub array category
	*-----------------------------------------------------------------------*/	
	function setArrayField($arrayField) {	
		if(is_array($arrayField)){
			foreach($arrayField as $key => $value){
				$this->field[$key] = stripslashes($value);
			}#endforeach
		}#endif	
	}
	/*-----------------------------------------------------------------------*
	* Function: getArraySubCategory
	* Parameter: WHERE condition
	* Return: Data will be sub array category
	*-----------------------------------------------------------------------*/
	function getArraySubCategory() {
		$arrIdCat = array();
		$productcategorysub = new Pick_Dao_ProductCategorySub();
		$arrObjSub = $productcategorysub->getObjects(1, "`productid`=".$this->getField('id'));
		if(isset($arrObjSub['data'])){
			foreach($arrObjSub['data'] as $objSub){
				$arrIdCat[] = $objSub->getField('categoryid');
			}
		}
		return $arrIdCat;
	}
	/*-----------------------------------------------------------------------*
	* Function: getArrayArea
	* Parameter: WHERE condition
	* Return: Data will be sub array category
	*-----------------------------------------------------------------------*/
	function getArrayArea() {
		$arrIdCat = array();
		$productarea = new Pick_Dao_ProductArea();
		$arrObj = $productarea->getObjects(1, "`productid`=".$this->getField('id'));
		if(isset($arrObj['data'])){
			foreach($arrObj['data'] as $obj){
				$arrIdCat[] = $obj->getField('categoryid');
			}
		}
		return $arrIdCat;
	}			
	/*-----------------------------------------------------------------------*
	* Function: getCurrency
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/
	function getCurrency() {
		$arrIdCat = array();
		$currency = new Pick_Dao_Currency();
		$objCurrency = $currency->getObject($this->getField('currencyid'));
		if(is_object($objCurrency)){
			return $objCurrency;
		}
		return;
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/	
	function getUrl($module=""){
		$modules = new Pick_Core_Modules();
		$objModule = $modules->getObjectFromCode($module);
		if(is_object($objModule)){
			return 	base_url()."/".$objModule->getField('slug')."/".$this->getField('slug')."/id/".$this->getField('id');
		}
		return base_url();
	}
	/*-----------------------------------------------------------------------*
	* Function: getFullUrlAvatar
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/				
	function getFullUrlAvatar($folder = '', $type = 'avatar', $nameAvatar = ""){
        if($nameAvatar != '')
			return base_url() ."/upload/images/". $folder ."/". $type . '/' . $nameAvatar;
        return base_url() ."/upload/images/". $folder ."/". $type . '/' . $this->getField('avatar');
	}
	/*-----------------------------------------------------------------------*
	* Function: getAvatar
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/				
	function getAvatar($nameAvatar = "", $type = 'storage'){
        if($nameAvatar != '')
			return base_url() ."/upload/images/". $type . '/' . $nameAvatar;
        return base_url() ."/upload/images/". $type . '/' . $this->getField('avatar');
	}
	/*-----------------------------------------------------------------------*
	* Function: getAvatar
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/				
	function getBackground($nameAvatar = "", $type = 'storage'){
        if($nameAvatar != '')
			return base_url() ."/upload/images/". $type . '/' . $nameAvatar;
        return base_url() ."/upload/images/". $type . '/' . $this->getField('background');
	}
	/*-----------------------------------------------------------------------*
	* Function: getFullUrlBackGround
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/
	function getFullUrlBackGround($nameAvatar = ""){
        if($nameAvatar != '')
			return base_url() . "/upload/images/background/" . $nameAvatar;
        return base_url() . "/upload/images/background/" . $this->getField('background');
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		
	function getFullUrlAvatarSlider($folder = '', $type = 'avatar', $nameAvatar = ""){
        if($nameAvatar != '')
			return base_url() ."/upload/images/". $folder ."/". $type . '/' . $nameAvatar;
        return base_url() ."/upload/images/". $folder ."/". $type . '/' . $this->getField('avatarslider');
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/			
	function getFullUrlAvatarFirst($folder = '', $type = 'avatar'){
		$arrAvatar = unserialize($this->getField('avatar'));
		if(count($arrAvatar)>0){
			$firstImg = array_shift($arrAvatar);
			if($firstImg==""){
				$firstImg = $arrAvatar;
			}
			return base_url() ."/upload/images/". $folder ."/". $type . '/' . $firstImg;
		}
		return '';
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		
	function getArrayAvatar($flag = 0){
		$arrAvatar = unserialize($this->getField('avatar'));
		$firstImg = '';
		$allAvatar = $arrAvatar;
		if(count($arrAvatar)>0)
			$firstImg = array_shift($arrAvatar);
		switch($flag){
			case 1: return $firstImg;
			case 2: return $arrAvatar;
			default: return $allAvatar;
		}                    
		return '';
	} 
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		        
	function getFullUrlVideo(){
		return base_url() ."/upload/images/". $folder ."/". STORAGE . '/' . $this->getField('video');
	}
	/*-----------------------------------------------------------------------*
	* Function: getFullUrlFile
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		        
	function getFullUrlFile($namefield=""){
        if(!empty($namefield)){
            return base_url() ."/upload/files/". $this->getField($namefield);
        }
        return;
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/				
	function serializeField($field){
		return serialize($this->getField($field));	
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/			
	function unSerializeField($field){
		return unserialize($this->getField($field));	
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/					
	function getFieldLang($key) {
		$key = $this->lang."_".$key;
		return $this->field[$key];
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/			
	function setFieldLang($key, $nValue) {
		$key = $this->lang."_".$key;
		$this->field[$key]=$nValue;
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		
	function getFieldLangAdmin($lang, $key) {
		$key = $lang."_".$key;
		return $this->field[$key];
	}
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/			
	function setFieldLangAdmin($lang, $key, $nValue) {
		$key = $lang."_".$key;
		$this->field[$key]=$nValue;
	}	
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		
	function getStatusText() {
		return l('status_text_'.$this->getField('status'));
	}	
	/*-----------------------------------------------------------------------*
	* Function: getUrl
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		
	function getExtFileVideo() {
			$ext = explode(".", $this->getField('video'));
			$ext = strtolower($ext[count($ext)-1]);
		return $ext;
	}		
	/*-----------------------------------------------------------------------*
	* Function: deleteAvatar
	* Parameter: WHERE condition is currentid
	* Return: Data will be return is object currency
	*-----------------------------------------------------------------------*/		
	function deleteAvatar($opinfo = ''){
		if(is_object($opinfo)){
			$dirnews 		 = IMAGES."/".$opinfo->getField('code');
		}else{
			$dirnews 		 = IMAGES."/".$opinfo;	
		}
		$gallery_avatar  = $dirnews."/".AVATAR;
		$gallery_medium  = $dirnews."/".MEDIUM;
		$gallery_big 	 = $dirnews."/".BIG;
		$gallery_storage = $dirnews."/".STORAGE;
				
		$ok1 = unlink($gallery_avatar .'/'. $this->getField('avatar'));
		$ok2 = unlink($gallery_medium .'/'. $this->getField('avatar'));
		$ok3 = unlink($gallery_big 	  .'/'. $this->getField('avatar'));
		$ok4 = unlink($gallery_storage.'/'. $this->getField('avatar'));
		if($ok1 && $ok2 && $ok3 && $ok4)
			return true;
		return false;
	}	
	/*-----------------------------------------------------------------------*
	* Function: deleteVideo
	* Parameter: $opinfo is a object infomation of the module
	* Return: true or false
	* Note: Only use for admin
	*-----------------------------------------------------------------------*/		
	function deleteVideo($opinfo){
		if(is_object($opinfo)){
			$dirnews 		 = IMAGES."/".$opinfo->getField('code');
		}else{
			$dirnews 		 = IMAGES."/".$opinfo;	
		}
		$gallery_storage = $dirnews."/".STORAGE;		
		$ok1 = unlink($gallery_storage .'/'. $this->getField('video'));
		if($ok1)
			return true;
		return false;
	}			
	function isEnabled() {
		return ($this->getField('status') == 1?1:0);
	}
	
	function isDeleted() {
		return ($this->getField('status') == 2?1:0);
	}
	
	function isDisabled() {
		return ($this->getField('status') == 0?1:0);
	}	
}	
?>